Yacon.component.TMultiGridBody = function(conText){
	
	Yacon.apply(this,conText);
	
	this.baseCreate();
	
};

Yacon.component.TMultiGridBody.prototype = {
		
	baseCreate: function(){
		
		if(!Yacon.isEmpty(this.view)){
			
			/* 标题固定 */
			this.isTitleFix = this.view.isTitleFix;
			/* 滚动处理 */
			this.isScroll = this.view.isScroll;
			
			this.title = this.view.data.title;
			
			this.body = this.view.data.data;
			
		}
		
	},
	
	/* 获取HTML信息 */	
	getHtml: function(){
		
		var html = "";
		
		html += "<div class='panel-body row-ht-auto col multigrid'>";
		
		html += this.getFormHtml();
		
		/* 滚动标题 */
		if(!Yacon.isTrue(this.isTitleFix)){			
			html += this.getScrollHtml();			
		}
		
		/* 固定标题 */
		else{			
			html += this.getFixHtml();			
		}
		
		html += "</div>";
			
		return html;
		
	},
	
	/* 固定标题 */
	getFixHtml: function(){
		
		var html = "";
		
		html += "<div class='table-responsive row-ht-auto col' style='min-width:100%;overflow-x: auto;overflow-y:hidden;' ";
		
		if(!Yacon.isEmpty(this.view.tableMenu)){
			html += " data-toggle='context' data-target='#"+this.parent.elementID+"-table-menu' ";
		}
		
		html += ">";	
			
		html += "<div class='row-ht-fix'" ;
		
		if(Yacon.isEmpty(this.view.tableWidth)){
			html +=	" style='width: 100%;' " ;
		}else{
			html +=	" style='width: "+this.view.tableWidth+";' " ;
		}
		
		html +=	" >";
		
		html += "<table class='table clear-margin-bottom fix table-bordered table-striped table-hover' >";
		
		/* 标题 */
		html += this.getHeadHtml();
		
		html += "</table>";
		
		html += "</div>";
		
		html += "<div class='row-ht-auto' ";	
		
		if(Yacon.isEmpty(this.view.tableWidth)){
			html +=	" style='width: 100%;overflow-y:auto;' " ;
		}else{
			html +=	" style='width: "+this.view.tableWidth+";overflow-y:auto;' " ;
		}
		
		html += ">";
		
		html += "<table class='table clear-margin-bottom fix table-bordered table-striped table-hover' >";
		
		/* 数据 */
		html += this.getBodyHtml();
		
		html += "</table>";		
		html += "</div>";
			
		html += "</div>";	
		
		return html;
		
	},
	
	/* 滚动标题 */
	getScrollHtml: function(){
		
		var html = "";
					
		html += "<div class='table-responsive row-ht-auto col' style='min-width:100%;overflow-x: auto;overflow-y:auto;' ";
		
		if(!Yacon.isEmpty(this.view.tableMenu)){
			html += " data-toggle='context' data-target='#"+this.parent.elementID+"-table-menu' ";
		}
		
		html += ">";	
			
		html += "<div class='row-ht-fix'" ;
		
		if(Yacon.isEmpty(this.view.tableWidth)){
			html +=	" style='width: 100%;' " ;
		}else{
			html +=	" style='width: "+this.view.tableWidth+";' " ;
		}
		
		html +=	" >";
		
		html += "<table class='table clear-margin-bottom fix table-bordered table-striped table-hover' >";
		
		/* 标题 */
		html += this.getHeadHtml();
		
		/* 数据 */
		html += this.getBodyHtml();
		
		html += "</table>";
		
		html += "</div>";
			
		html += "</div>";	
		
		return html;
	},
	
	getHeadHtml: function(){
			
		var html = "";
		
		html += "<thead ";
		
		if(Yacon.isEmpty(this.view.tableMenu) && !Yacon.isEmpty(this.parent.titleMenu)){
			html += " data-toggle='context' data-target='#"+this.parent.elementID+"-title-menu' ";
		}
		
		html += ">"
		
		this.finalIdx = 0;	
			
		for(var row=0;row<this.title.length;row++){
			
			html += "<tr>";
			
			for(var col=0;col<this.title[row].length;col++){
				
				var td = this.title[row][col];
				
				if(row == 0){
					this.finalIdx += Yacon.getInt(td.colSpan);
				}
				
				html += "<td colspan='"+td.colSpan+"' rowspan='"+td.rowSpan+"' ";
				html += " fieldName='"+td.fieldName+"' fieldValue='"+td.fieldValue+"' ";
				
				html += " align='"+td.align+"' valign='"+td.valign+"' ";
				
				var style = "";
				
				if(!Yacon.isEmpty(td.fontColor)){
					style = "color:"+td.fontColor+";"
				}
				
				if(!Yacon.isEmpty(td.backColor)){
					style += " background:"+td.backColor+"; ";
				}
				
				if(!Yacon.isEmpty(td.width)){
					style += " width:"+td.width+"; ";
				}
				
				if(!Yacon.isEmpty(td.height)){
					style += " height:"+td.height+"; ";
				}
				
				if(!Yacon.isEmpty(style)){
					html += " style='"+style+"' ";
				}
				
				html += " class='title-field' ";
				
				html += ">";
				
				html += td.fieldValue;
				
				html += "</td>";
				
			}
			
			html += "</tr>";
			
		}
		
		if(Yacon.isTrue(this.isTitleFix)){
			
			html +="<tr class='tmp-table-row'>"
				
			for(var i=0;i<this.finalIdx;i++){
				html += "<td></td>";
			}
				
			html += "</tr>"
		}
		
		html += "</thead>";
		
		return html;
		
	},
	
	getBodyHtml: function(){
		
		var html = "";
		
		html += "<tbody ";
		
		if(Yacon.isEmpty(this.view.tableMenu) && !Yacon.isEmpty(this.view.dataMenu)){
			html += " data-toggle='context' data-target='#"+this.parent.elementID+"-data-menu' ";
		}
		html += ">";
		
		for(var row=0;row<this.body.length;row++){
			
			html += "<tr>";
			
			for(var col=0;col<this.body[row].length;col++){
				
				var td = this.body[row][col];
				
				html += "<td colspan='"+td.colSpan+"' rowspan='"+td.rowSpan+"' ";
				html += " fieldName='"+td.fieldName+"' fieldValue='"+td.fieldValue+"' ";
				html += " align='"+td.align+"' valign='"+td.valign+"' ";
				html += " fieldCol='"+col+"' fieldRow='"+row+"' ";
							
				var style = "";
				
				if(!Yacon.isEmpty(td.fontColor)){
					style = " color:"+td.fontColor+"; "
				}
				
				if(!Yacon.isEmpty(td.backColor)){
					style += " background:"+td.backColor+"; ";
				}
				
				if(!Yacon.isEmpty(style)){
					html += " style='"+style+"' ";
				}
				
				//标题字段处理
				if(td.type == "title"){
					html += " class='title-field' ";
				}else{
					html += " class='data-field' ";
				}
				
				html += ">";
				
				html += td.fieldValue;
				
				html += "</td>";
				
			}
			
			html += "</tr>";
			
		}
		
		html += "</tbody>";
		
		return html;
		
	},
	
	/* 查询部分 */
	getFormHtml: function(){
		
		var html = "";
		
		var formEle = this.view.form;
		
		if(!Yacon.isEmpty(formEle)){
			
			html += "<div class='table-form row-ht-fix' style='margin-bottom:25px;'>";
			
			var findform = new Yacon.widget.TForm({view:formEle,page:this.parent.page,parent:this.parent});		
			this.parent.page.addComponet(findform);		
			this.parent.findform = findform;
			findform.page = this.parent.page;
			
			html += findform.getHtml();
			
			html += "</div>";
			
		}
		
		return html;
		
	},
	
	refresh: function(){
		
		this.view = this.parent.view;
		
		this.baseCreate();
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		
		var html = this.getHeadHtml() + this.getBodyHtml();
		
		$(search+" .panel-body .table-responsive table.table").empty();
		$(search+" .panel-body .table-responsive table.table").append(html);
		
		/* 重新布局 */
		this.parent.page.layoutOuter(search+" .panel-body");
		
	},
	
	/* 绑定动作 */
	addEvents: function(){
		
		if(Yacon.isTrue(this.isTitleFix)){
			this.autoWidth();
		}
		
		self.headTimer = null;
		self.dataTimer = null;
		
		//数据单击事件
		this.addDataClick();
		
		//标题单击事件
		this.addHeadClick();
		
		//数据双击事件
		this.addDataDbClick();
		
		//标题双击事件
		this.addHeadDbClick();
		
	},
	
	autoWidth: function(){
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		
		var TRs = $(search +" .panel-body .table-responsive tbody tr");
		var HR = $(search +" .panel-body .table-responsive thead .tmp-table-row");
		if(TRs.length>0){
			var TR = TRs.eq(0);
			var TDs = TR.find("td");
			var HDs = HR.find("td");
			
			for(var i=0;i<TDs.length;i++){
				
				if(i != (TDs.length-1)){
					TDs.eq(i).css("width",HDs.eq(i).css("width"));
				}		
				
			}
			
		}
		
		$(search +" .panel-body .table-responsive thead .tmp-table-row").remove();
		
	},
	
	addHeadClick: function(){
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		var self = this;
		
		$(search+" .panel-body .table-responsive thead td").unbind("click");
		$(search+" .panel-body .table-responsive thead td").click(function(){
			
			if(!Yacon.isEmpty(self.headTimer)){
				clearTimeout(self.headTimer);
			}
			
			var childSelf = this;
			
			self.headTimer = setTimeout(function(){
				
				var activeCell = new Yacon.component.TMultiCell({
					activeCell : $(childSelf),
					page : self.parent.page,
					parent: self,
					search: search
				});
				
				if(!Yacon.isEmpty(self.parent.view.titleClick)){
					Yacon.callBack(self.parent.view.titleClick, [activeCell,page]);
				}
				
				else if(!Yacon.isEmpty(self.parent.view.tableClick)){
					Yacon.callBack(self.parent.view.tableClick, [activeCell,page]);
				}
				
			},300);
			
		});
		
	},
	
	addDataClick: function(){
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		var self = this;
		
		$(search+" .panel-body .table-responsive tbody td").unbind("click");
		$(search+" .panel-body .table-responsive tbody td").click(function(){
			
			if(!Yacon.isEmpty(self.dataTimer)){
				clearTimeout(self.dataTimer);
			}
			
			var childSelf = this;
			
			self.dataTimer = setTimeout(function(){
				
				self.addModeEvents($(childSelf));	
				
				var activeCell = new Yacon.component.TMultiCell({
					activeCell : $(childSelf),
					page : self.parent.page,
					parent: self,
					search: search
				});
				
				if(!Yacon.isEmpty(self.parent.view.cellClick)){
					Yacon.callBack(self.parent.view.cellClick, [activeCell,page]);
				}
				
				else if(!Yacon.isEmpty(self.parent.view.rowClick)){
					Yacon.callBack(self.parent.view.rowClick, [activeCell,page]);
				}
				
				else if(!Yacon.isEmpty(self.parent.view.colClick)){
					Yacon.callBack(self.parent.view.colClick, [activeCell,page]);
				}
				
				else if(!Yacon.isEmpty(self.parent.view.dataClick)){
					Yacon.callBack(self.parent.view.dataClick, [activeCell,page]);
				}
				
				else if(!Yacon.isEmpty(self.parent.view.tableClick)){
					Yacon.callBack(self.parent.view.tableClick, [activeCell,page]);
				}
				
			},300);
			
		});
		
	},
	
	addHeadDbClick: function(){
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		var self = this;
		
		$(search+" .panel-body .table-responsive thead td").dblclick(function(){
			
			if(!Yacon.isEmpty(self.headTimer)){
				clearTimeout(self.headTimer);
			}
				
			var activeCell = new Yacon.component.TMultiCell({
				activeCell : $(this),
				page : self.parent.page,
				parent: self,
				search: search
			});
			
			if(!Yacon.isEmpty(self.parent.view.titleDbClick)){
				Yacon.callBack(self.parent.view.titleDbClick, [activeCell,page]);
			}
			
			else if(!Yacon.isEmpty(self.parent.view.tableDbClick)){
				Yacon.callBack(self.parent.view.tableDbClick, [activeCell,page]);
			}
				
		});
		
	},
	
	addDataDbClick: function(){
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		var self = this;
		
		$(search+" .panel-body .table-responsive tbody td").dblclick(function(){
			
			if(!Yacon.isEmpty(self.dataTimer)){
				clearTimeout(self.dataTimer);
			}
						
			self.addModeEvents($(this));	
			
			var activeCell = new Yacon.component.TMultiCell({
				activeCell : $(this),
				page : self.parent.page,
				parent: self,
				search: search
			});
			
			if(!Yacon.isEmpty(self.parent.view.cellDbClick)){
				Yacon.callBack(self.parent.view.cellDbClick, [activeCell,page]);
			}
			
			else if(!Yacon.isEmpty(self.parent.view.rowDbClick)){
				Yacon.callBack(self.parent.view.rowDbClick, [activeCell,page]);
			}
			
			else if(!Yacon.isEmpty(self.parent.view.colDbClick)){
				Yacon.callBack(self.parent.view.colDbClick, [activeCell,page]);
			}
			
			else if(!Yacon.isEmpty(self.parent.view.dataDbClick)){
				Yacon.callBack(self.parent.view.dataDbClick, [activeCell,page]);
			}
			
			else if(!Yacon.isEmpty(self.parent.view.tableDbClick)){
				Yacon.callBack(self.parent.view.tableDbClick, [activeCell,page]);
			}
			
		});
		
	},
	
	/* 形式变化 */
	addModeEvents: function(tdObj){

		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		
		var choice = this.parent.view.choice;
		var clickMode = this.parent.view.clickMode;
		
		if(choice == "check"){
					
			if(clickMode == "cell"){
				tdObj.toggleClass("active");
			}
			
			else if(clickMode == "row"){
				
				var row = tdObj.attr("fieldRow");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldRow='"+row+"']").toggleClass("active");
				
			}
			
			else if(clickMode == "col"){
				
				var col = tdObj.attr("fieldCol");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldCol='"+col+"']").toggleClass("active");
				
			}
			
			else if(clickMode == "row-col"){
				var row = tdObj.attr("fieldRow");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldRow='"+row+"']").toggleClass("active");
				var col = tdObj.attr("fieldCol");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldCol='"+col+"']").toggleClass("active");
				tdObj.toggleClass("active");
			}
			
			
		}else{
			$(search+" .panel-body .table-responsive table.table tbody td").removeClass("active");
			
			if(clickMode == "cell"){
				tdObj.toggleClass("active");
			}
			
			else if(clickMode == "row"){
				
				var row = tdObj.attr("fieldRow");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldRow='"+row+"']").toggleClass("active");
				
			}
			
			else if(clickMode == "col"){
				
				var col = tdObj.attr("fieldCol");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldCol='"+col+"']").toggleClass("active");
				
			}
			
			else if(clickMode == "row-col"){
				var row = tdObj.attr("fieldRow");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldRow='"+row+"']").toggleClass("active");
				var col = tdObj.attr("fieldCol");
				$(search+" .panel-body .table-responsive table.table tbody td[fieldCol='"+col+"']").toggleClass("active");
				tdObj.toggleClass("active");
			}
			
		}
	}
		
};